package com.example.mall.dao;

import com.example.mall.model.ShoppingCart;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class ShoppingCartDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    public void insert(ShoppingCart shoppingCart) {
        String sql = "insert into shoppingCart (buyerID, cID) values (?, ?)";
        jdbcTemplate.update(sql, shoppingCart.getBuyerID(), shoppingCart.getcID());
    }

    public void delete(Integer buyerID, Integer cID) {
        String sql = "delete from shoppingCart where buyerID = ? and cID = ?";
        jdbcTemplate.update(sql, buyerID, cID);
    }

    public List<ShoppingCart> getByBid(Integer buyerID) {
        String sql = "select * from shoppingCart where buyerID = ? lock in share mode";
        RowMapper<ShoppingCart> rowMapper = new BeanPropertyRowMapper<>(ShoppingCart.class);
        return jdbcTemplate.query(sql, rowMapper, buyerID);
    }

}
